package labox.innovation.gameserver.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastMap;
import labox.innovation.config.Config;

/**
 * This class handles all chat handlers
 *
 * @author  durgus
 */
public class ChatHandler {

	private static final Logger _log = LoggerFactory.getLogger(ChatHandler.class.getName());

	private FastMap<Integer, IChatHandler> _datatable;

	public static ChatHandler getInstance() {
		return SingletonHolder._instance;
	}

	/**
	 * Singleton constructor
	 */
	private ChatHandler() {
		_datatable = new FastMap<Integer, IChatHandler>();
	}

	/**
	 * Register a new chat handler
	 * @param handler
	 */
	public void registerChatHandler(IChatHandler handler) {
		int[] ids = handler.getChatTypeList();
		for (int i = 0; i < ids.length; i++) {
			if (_log.isDebugEnabled())
				_log.debug("Adding handler for chat type " + ids[i], Config.SERVER_ID);
			_datatable.put(ids[i], handler);
		}
	}

	/**
	 * Get the chat handler for the given chat type
	 * @param chatType
	 * @return
	 */
	public IChatHandler getChatHandler(int chatType) {
		return _datatable.get(chatType);
	}

	/**
	 * Returns the size
	 * @return
	 */
	public int size() {
		return _datatable.size();
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final ChatHandler _instance = new ChatHandler();
	}
}